package com.example.spring.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.example.spring.demo.entity.UserToken;
import com.example.spring.demo.mapper.TokenMapper;
import com.example.spring.demo.service.TokenService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/12/31 14:56
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class TokenServiceImpl implements TokenService {
    private final TokenMapper tokenMapper;
    @Override
    public void saveToken(UserToken userToken) {
        String token = getToken(userToken.getTokenKey());
        if (StrUtil.isNotEmpty(token)) {
            int delUserToken = delUserToken(userToken.getUserId());
            log.info("删除token：当前删除了用户：{}的{}条token",userToken.getUserId(), delUserToken);
        }
        tokenMapper.saveToken(userToken);
    }

    @Override
    public String getToken(String tokenKey) {
        return tokenMapper.getToken(tokenKey);
    }

    @Override
    public int delUserToken(UserToken userToken) {
        int delUserToken = tokenMapper.delUerToken(userToken);
        log.info("删除token：当前删除了用户：{}的{}条token",userToken.getUserId(), delUserToken);
        return delUserToken;
    }

    @Override
    public int delUserToken(Integer userId) {
        int delToken = tokenMapper.delUserToken(userId);
        return 0;
    }
}
